package com.android.gufengmall.mall;


import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

import com.android.gufengmall.App;
import com.android.gufengmall.BaseActivity;
import com.android.gufengmall.R;
import com.android.gufengmall.adapter.CarouselAdapter;
import com.android.gufengmall.bean.CarouselBean;
import com.android.gufengmall.bean.GoodsBean;
import com.android.gufengmall.bean.OrderBean;
import com.android.gufengmall.gen.DaoSession;
import com.android.gufengmall.gen.GoodsBeanDao;
import com.android.gufengmall.gen.OrderBeanDao;
import com.android.gufengmall.utils.LogUtils;
import com.google.android.material.snackbar.Snackbar;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;

import java.util.List;

/**
 * 商品详情页面
 */
public class MallDetailsActivity extends BaseActivity {

    private final static String TAG = "MallDetailsActivity";

    private Banner mBanner;
    private CarouselAdapter mCarouselAdapter;

    private int mPosition;
    private String mGoodsName;

    private App mMyApp;
    private DaoSession mDaoSession;
    private GoodsBeanDao mGoodsBeanDao;
    private OrderBeanDao mOrderBeanDao;

    private TextView mTvTitle;
    private TextView mTvInfo;
    private TextView mTvPrice;
    private TextView mTvAmount;
    private Button mBtnJoin;
    private GoodsBean mGoodsBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mall_details);

        initView();
        initData();
        initListener();
    }

    private void initListener() {
        findViewById(R.id.img_back).setOnClickListener(v -> {
            finish();
        });
        mBtnJoin.setOnClickListener(v -> {
            OrderBean orderBean = new OrderBean();
            orderBean.setOrderId(mPosition);
            orderBean.setOrderPrice(mGoodsBean.getGoods_price());
            orderBean.setOrderInfo(mGoodsBean.getGoods_info());
            orderBean.setOrderName(mGoodsBean.getGoods_name());
            orderBean.setOrderAmount(mGoodsBean.getGoods_amount());
            orderBean.setOrderImgShop(mGoodsBean.getGoods_imgShop());
            mOrderBeanDao.insert(orderBean);
            showToast("加入购物车成功");
        });
    }

    private void initData() {
        mPosition = getIntent().getExtras().getInt("position");
        mGoodsName = getIntent().getExtras().getString("goodsName");
        LogUtils.d("position：" + mPosition);
        mCarouselAdapter = new CarouselAdapter(CarouselBean.getMallDetailCarouselData(mPosition));

        mMyApp = (App) this.getApplication();
        mDaoSession = mMyApp.getDaoSession();
        mGoodsBeanDao = mDaoSession.getGoodsBeanDao();
        mOrderBeanDao = mDaoSession.getOrderBeanDao();

        List<GoodsBean> list = mGoodsBeanDao.queryBuilder()
                .where(GoodsBeanDao.Properties.Goods_name.eq(mGoodsName))
                .list();
        mGoodsBean = list.get(0);
        mTvTitle.setText(mGoodsBean.getGoods_name());
        mTvInfo.setText(mGoodsBean.getGoods_info());
        mTvPrice.setText(mGoodsBean.getGoods_price() + "");
        mTvAmount.setText(mGoodsBean.getGoods_amount() + "");

        mBanner.setAdapter(mCarouselAdapter)
                .addBannerLifecycleObserver(this)//添加生命周期观察者
                .setIndicator(new CircleIndicator(this))//设置指示器
                .setOnBannerListener((data, position) -> {
                    Snackbar.make(mBanner, ((CarouselBean) data).title, Snackbar.LENGTH_SHORT).show();
                });
    }

    private void initView() {
        mBanner = findViewById(R.id.banner);
        mTvTitle = findViewById(R.id.tv_goods_title);
        mTvInfo = findViewById(R.id.tv_goods_info);
        mTvPrice = findViewById(R.id.tv_new_price);
        mTvAmount = findViewById(R.id.tv_amount);
        mBtnJoin = findViewById(R.id.btn_join);
    }
}